package com.testing.sunnycommon.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @Classname SunnyWebSocketConfiguration
 * @Description 类型说明
 * @Date 2023/6/9 21:14
 * @Created by 特斯汀Roy
 */
@Configuration
//允许websocket的消息广播
@EnableWebSocketMessageBroker
public class SunnyWebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        //指定服务器进行广播的地址，如果服务器要对外发消息，要走的是/topic开头的路径
        //客户端需要订阅subscribe这个路径，从而能够接收广播。
        config.enableSimpleBroker("/topic");
        //客户端如果要向服务器发消息，路径是以/app开头
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    //注册一个stomp协议端点（服务器），这个服务器的连接url就是/sunnyWebSocket
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/sunnyWebSocket")
                //设置允许所有的客户端连接websocket
                .setAllowedOriginPatterns("*")
                //允许前端sockjs连接到这个端点
                .withSockJS();
    }


}
